/*
 ============================================================================
 Name		: DownloadEngine.cpp
 Author	  : JohnLZeng
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CDownloadEngine implementation
 ============================================================================
 */

#include "DownloadEngine.h"

#include "QWString.h"
#include "cx_moduleinterface.h"
#include "QQReaderMessageId.h"
#include "QQReaderModuleId.h"
#include "cx_string.h"
#include "CommonFileUtil.h"

CDownloadEngine::CDownloadEngine()
	{
	// No implementation required
	}

CDownloadEngine::~CDownloadEngine()
	{
	}

CDownloadEngine* CDownloadEngine::NewLC()
	{
	CDownloadEngine* self = new (ELeave) CDownloadEngine();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CDownloadEngine* CDownloadEngine::NewL()
	{
	CDownloadEngine* self = CDownloadEngine::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CDownloadEngine::ConstructL()
	{

	}
void CDownloadEngine::Init()
	{
	iStoreFile.InitDownloadList();
	}

void CDownloadEngine::Release()
	{
	iStoreFile.ReleaseDownloadList();
	}

TInt CDownloadEngine::GetAll(ArrayDownloadList& aArray)
	{
	iStoreFile.GetDownloadInfoList(aArray);
	}

TInt CDownloadEngine::Save(ArrayDownloadList& aArray)
	{
	iStoreFile.PutDownloadInfoList(aArray);
	}

//CDownloadInfoData* CDownloadEngine::AddTask(ArrayDownloadList& aArray,
//		const TDesC& aUrl)
//	{
//	CDownloadInfoData* download = CDownloadInfoData::NewL();
//	download->SetState(CDownloadInfoData::EDownloadStateStop);
//	download->SetUrl(aUrl);
//
//	aArray.Append(download);
//
//	iStoreFile.PutDownloadInfoList(aArray);
//
//	return download;
//	}

TInt CDownloadEngine::AddTask(ArrayDownloadList& aArray,const TInt& aPos,const TDesC& aUrl)
	{
	if (iStoreFile.IsExistInDownloadList(aUrl))
		{
		return KErrAlreadyExists;
		}
	
	CDownloadInfoData* download = CDownloadInfoData::NewL();
	download->SetState(CDownloadInfoData::EDownloadStateWaiting);
	download->SetUrl(aUrl);
	aArray.Insert(download,aPos);
	
	CQWString url;
	url.SetData((LPCWSTR) (aUrl.Ptr()), aUrl.Length());// for browser lean added 091119

	cx_sendmessage(EMessage_BookCity_Browse_Download,
			(unsigned int) (&url), 0, EModule_Controller_BookCity, -1);

	return KErrNone;
	}

TInt CDownloadEngine::ConvertToListFormat(const ArrayDownloadList& aArray,
		CDesCArray* aDesC)
	{
	for (TInt i = aArray.Count()-1; i >=0; i--)
		{
		CDownloadInfoData* brief = aArray[i];
		TBuf<MAX_TITLE_LENGTH> title;
		title.AppendNum(brief->GetState());
		title.Append('\t');
		title.Append(brief->GetFile()->Des());
		title.Append(_L("\t"));
		title.AppendNum(brief->GetProgress()/10);
		title.Append('.');
		title.AppendNum(brief->GetProgress()%10);
		title.Append(_L(" %"));
		aDesC->AppendL(title);
		}
	}

TInt CDownloadEngine::ConvertToListFormat(CDownloadInfoData* aItem,
		TDes& aFormat)
	{
	aFormat.AppendNum(aItem->GetState());
	aFormat.Append('\t');
	aFormat.Append(aItem->GetFile()->Des());
	aFormat.Append(_L("\t"));
	aFormat.AppendNum(aItem->GetProgress());
	aFormat.Append(_L(" %"));
	}

TBool CDownloadEngine::IsTaskRunningOrWaiting(ArrayDownloadList& aArray,
		const TInt& aPos)
	{
	CDownloadInfoData* info = aArray[aPos];
	if (info->GetState() == CDownloadInfoData::EDownloadStateRunning
			|| info->GetState() == CDownloadInfoData::EDownloadStateWaiting)
		{
		return ETrue;
		}
	else
		return EFalse;
	}

TBool CDownloadEngine::ExistRunningTask(ArrayDownloadList& aArray)
	{
	for (TInt i = 0; i < aArray.Count(); i++)
		{
		CDownloadInfoData* info = aArray[i];
		if (info->GetState() == CDownloadInfoData::EDownloadStateRunning)
			{
			return ETrue;
			}
		}
	return EFalse;
	}

void CDownloadEngine::StartTask(ArrayDownloadList& aArray, const TInt& aPos)
	{
	CDownloadInfoData* task = aArray[aPos];
	
	TFileName filename;
	CCommonFileUtil::GetDetailFolderEFirst(filename);
	filename.Append(task->GetFile()->Des());
		
	CQWString filename2;
	filename2.SetData((LPCWSTR) (filename.Ptr()), filename.Length());// for browser lean added 091119
		
	cx_sendmessage(EMessage_BookCity_Download_Operate,(unsigned int)(&filename2),
			0,EModule_Controller_BookCity,-1);
	
//	if (task->GetState() == CDownloadInfoData::EDownloadStateStop)
//		{
//		TBool exist = ExistRunningTask(aArray);
//		if (exist)
//			{
//			task->SetState(CDownloadInfoData::EDownloadStateWaiting);
//			}
//		else
//			{
//			task->SetState(CDownloadInfoData::EDownloadStateRunning);
//			}
//		task->SetUpdate(ETrue);
//
//		iStoreFile.PutDownloadInfoList(aArray);
//
//		CQWString url;
//		TPtrC ptr = task->GetUrl()->Des();
//		url.SetData((LPCWSTR) (ptr.Ptr()), ptr.Length());// for browser lean added 091119
//
//		cx_sendmessage(EMessage_BookCity_Browse_Download,
//				(unsigned int) (&url), 0, EModule_Controller_BookCity, -1);
//		}
	}
void CDownloadEngine::PauseTask(ArrayDownloadList& aArray, const TInt& aPos)
	{
	CDownloadInfoData* task = aArray[aPos];
	if (task->GetState() != CDownloadInfoData::EDownloadStateStop)
		{
		task->SetState(CDownloadInfoData::EDownloadStateStop);
		task->SetUpdate(ETrue);

		iStoreFile.PutDownloadInfoList(aArray);
		}
	}

void CDownloadEngine::StartAll(ArrayDownloadList& aArray)
	{
	for (TInt i = 0; i < aArray.Count(); i++)
		{
		CDownloadInfoData* task = aArray[i];
		task->SetState(CDownloadInfoData::EDownloadStateWaiting);
//		task->SetUpdate(ETrue);
		}
//	Save(aArray);
	
	cx_sendmessage(EMessage_BookCity_Download_StartAll, 0, 0,
			EModule_Controller_BookCity, -1);
	}
void CDownloadEngine::PauseAll(ArrayDownloadList& aArray)
	{
	for (TInt i = 0; i < aArray.Count(); i++)
		{
		CDownloadInfoData* task = aArray[i];
		task->SetState(CDownloadInfoData::EDownloadStateStop);
//		task->SetUpdate(ETrue);
		}
//	Save(aArray);
	
	cx_sendmessage(EMessage_BookCity_Download_PauseAll, 0, 0,
			EModule_Controller_BookCity, -1);
	}

void CDownloadEngine::ClearAll(ArrayDownloadList& aArray)
	{
	cx_sendmessage(EMessage_BookCity_Download_PauseAll, 0, 0,
			EModule_Controller_BookCity, -1);
	
	aArray.ResetAndDestroy();
	
	iStoreFile.RemoveAllDownloadInfoList();
	}

void CDownloadEngine::RemoveTask(ArrayDownloadList& aArray, const TInt& aPos)
	{
	CDownloadInfoData* task = aArray[aPos];
	aArray.Remove(aPos);

	iStoreFile.RemoveOneDownloadInfoList(task->GetUrlQ());

	TFileName filename;
	CCommonFileUtil::GetDetailFolderEFirst(filename);
	filename.Append(task->GetFile()->Des());
	
	CQWString filename2;
	filename2.SetData((LPCWSTR) (filename.Ptr()), filename.Length());// for browser lean added 091119

	CQBookCityDelete deleteO(filename2,QTRUE);
	cx_sendmessage(EMessage_BookCity_Download_Delete,(unsigned int)(&deleteO),0,EModule_Controller_BookCity,-1);
			
//		cx_sendmessage(EMessage_BookCity_Browse_CancelRequest,
//				(unsigned int) (&url), 0, EModule_Controller_BookCity, -1);
		
	delete task;
	}

void CDownloadEngine::RemoveCompeleteTask(ArrayDownloadList& aArray,const TInt& aPos)
	{
	CDownloadInfoData* task = aArray[aPos];
//	task->SetDelete(ETrue);
//	task->SetUpdate(ETrue);
//	iStoreFile.PutDownloadInfoList(aArray);	
	iStoreFile.RemoveOneDownloadInfoList(task->GetUrlQ());
	aArray.Remove(aPos);
	delete task;
	}

void CDownloadEngine::RemoveTaskAndFile(ArrayDownloadList& aArray,
		const TInt& aPos)
	{
	CDownloadInfoData* task = aArray[aPos];
	aArray.Remove(aPos);

	iStoreFile.RemoveOneDownloadInfoList(task->GetUrlQ());

	if (task->GetState() == CDownloadInfoData::EDownloadStateStop)
		{
		CQWString url;
		TPtrC ptr = task->GetUrl()->Des();
		url.SetData((LPCWSTR) (ptr.Ptr()), ptr.Length());// for browser lean added 091119

		cx_sendmessage(EMessage_BookCity_Browse_CancelRequest,
				(unsigned int) (&url), 0, EModule_Controller_BookCity, -1);
		}
	delete task;

	}
